import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GaoBo
 * Date: 2021-06-08
 * Time: 20:58
 */
class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }
}
public class BinaryTree {

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    // 前序遍历
    void preOrderTraversal(TreeNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);
    }


    // 中序遍历
    void inOrderTraversal(TreeNode root){
        if(root == null) {
            return;
        }
        inOrderTraversal(root.left);
        System.out.print(root.val+" ");
        inOrderTraversal(root.right);
    }

    // 后序遍历
    void postOrderTraversal(TreeNode root){
        if(root == null) {
            return;
        }
        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.print(root.val+" ");
    }

    // 遍历思路-求结点个数  前序遍历
    static int size = 0;
    void getSize1(TreeNode root) {
        if(root == null) {
            return;
        }
        size++;
        getSize1(root.left);
        getSize1(root.right);
    }

    // 子问题思路-求结点个数
    int getSize2(TreeNode root) {
        if(root == null){
            return 0;
        }
        return getSize2(root.left) + getSize2(root.right)+1;
    }

    // 遍历思路-求叶子结点个数
    static int leafSize = 0;
    void getLeafSize1(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafSize1(root.left);
        getLeafSize1(root.right);
    }

    // 子问题思路-求叶子结点个数
    int getLeafSize2(TreeNode root) {
        if(root == null) {
            return 0;
        }

        if(root.left == null && root.right == null) {
            return 1;
        }

        return getLeafSize2(root.left) + getLeafSize2(root.right);
    }

    //K 层
    int getKLevelSize(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelSize(root.left,k-1)
                + getKLevelSize(root.right,k-1);
    }

    /**
     *
     * @param root
     * @param val
     * @return
     */
    TreeNode find(TreeNode root, char val) {
        if(root == null) return null;
        if(root.val == val) return root;

        TreeNode ret = find(root.left,val);
        if(ret != null) {
            return ret;
        }
        ret = find(root.right,val);
        if(ret != null) {
            return ret;
        }
        return null;
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        //return  (getHeight(root.left) >  getHeight(root.right) ? getHeight(root.left)+1 :  getHeight(root.right)+1);
        return  (leftHeight >  rightHeight ? leftHeight+1 :  rightHeight+1);
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        //只要有一个不为空
        if(p == null && q != null) {
            return false;
        }
        if(p != null && q == null) {
            return false;
        }
        //走到这里 还有可能2个都为空
        if(p == null && q == null) {
            return true;
        }
        //都不为空
        if(p.val != q.val) {
            return false;
        }

        //p和q不为空  且对应的值 是相同的  那么去判断两棵树的左树和右树
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);

    }


    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null || subRoot == null) return false;

        if(isSameTree(root,subRoot)) return true;

        if(isSubtree(root.left,subRoot)) return true;

        if(isSubtree(root.right,subRoot)) return true;

        return false;
    }

    /**
     求高度的时间复杂度：O(n)
     */
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);

        return  (leftHeight > rightHeight ? leftHeight+1 : rightHeight+1);
    }
    //时间复杂度是多少？ o(n^2)
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftHeight =  maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        return
                Math.abs(leftHeight-rightHeight) < 2 && isBalanced(root.left) && isBalanced(root.right);
    }

    /**
     原理 就是在求高度的同时，只要某一个子树不满足就直接结束
     从下往上的方式
     */
    public int hight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = hight(root.left);
        int rightHeight =  hight(root.right);
       /*
       只要 当前根的左树 或者右树 不满足
       leftHeight == -1 || rightHeight==-1
       就不会进入if语句
        */
        if(leftHeight >= 0 && rightHeight >= 0 && Math.abs(leftHeight-rightHeight) <=1     )  {
            return Math.max(leftHeight,rightHeight)+1;
        }else{
            return -1;
        }
    }

    public boolean isBalanced2(TreeNode root) {
        return hight(root) >= 0;
    }


    public boolean isSymmetricChild(TreeNode leftTree,TreeNode rightTree) {
        if(leftTree == null && rightTree != null) {
            return false;
        }
        if(leftTree != null && rightTree == null) {
            return false;
        }
        if(leftTree == null && rightTree == null) {
            return true;
        }

        if(leftTree.val != rightTree.val) {
            return false;
        }

        return isSymmetricChild(leftTree.left,rightTree.right)
                && isSymmetricChild(leftTree.right,rightTree.left);

    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }
        return isSymmetricChild(root.left,root.right);
    }

    public TreeNode Mirror (TreeNode pRoot) {
        // write code here
        if(pRoot == null) {
            return pRoot;
        }
        if(pRoot.left == null && pRoot.right == null) {
            return pRoot;
        }

        TreeNode tmp = pRoot.left;
        pRoot.left = pRoot.right;
        pRoot.right = tmp;

        if(pRoot.left != null) {
            Mirror(pRoot.left);
        }
        if(pRoot.right != null) {
            Mirror(pRoot.right);
        }
        return pRoot;
    }

    // 层序遍历
    void levelOrderTraversal(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            System.out.print(top.val+" ");
            if(top.left != null) {
                queue.offer(top.left);
            }
            if(top.right!=null) {
                queue.offer(top.right);
            }
        }
        System.out.println();
    }

    /**
     *
     * @param root
     * @return
     */
    public List<List<Character>> levelOrder(TreeNode root) {
        //只是为了满足结果的要求 而创建的
        List<List<Character>> ret = new ArrayList<>();

        if(root == null) {
            return ret;
        }
        //队列的作用是用来实现层序遍历
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> list = new ArrayList<>();
            while (size != 0) {
                TreeNode top = queue.poll();
                list.add(top.val);
                if(top.left != null) {
                    queue.offer(top.left);
                }
                if(top.right != null) {
                    queue.offer(top.right);
                }
                size--;
            }
            ret.add(list);
        }
        return ret;
    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root) {
        if(root == null) return true;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode top = queue.poll();
            if(top != null) {
                queue.offer(top.left);
                queue.offer(top.right);
            }else{
                break;
            }
        }
        while (!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if(cur == null) {
                queue.poll();
            }else {
                return false;
            }
        }
        return true;
    }

    // 前序遍历
    void preOrderTraversalNor(TreeNode root) {
        if(root == null) return;
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();

        while (cur != null  || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }

            TreeNode top = stack.pop();
            cur = top.right;//L
        }
        System.out.println();
    }

    // 中序遍历
    void inOrderTraversalNor(TreeNode root) {
        if(root == null) return;
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();

        while(cur!=null ||  !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
        System.out.println();
    }

    // 后序遍历
    void postOrderTraversalNor1(TreeNode root) {

        if(root == null) return;
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;//用来指定 上一个被打印的元素

        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.peek();
            cur = top.right;
            if (cur == null || cur != pre) {
                TreeNode popNode = stack.pop();
                System.out.print(popNode.val + " ");
                pre = top;
            } else {
                cur = null;
            }
        }
    }

    void postOrderTraversalNor(TreeNode root) {
        if(root == null) return;
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;//用来指定 上一个被打印的元素

        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.peek();
            if (cur.right == null || cur.right == pre) {
                TreeNode popNode = stack.pop();
                System.out.print(popNode.val + " ");
                pre = cur;
                cur = null;
            } else {
                cur = cur.right;
            }
        }
        System.out.println();
    }


    public TreeNode prev = null;

    public void ConvertChild(TreeNode pCur) {
        if(pCur == null) {
            return;
        }
        ConvertChild(pCur.left);
        //打印
        pCur.left = prev;
        if(prev != null) {
            prev.right = pCur;
        }
        prev = pCur;
        ConvertChild(pCur.right);
    }

    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree == null) return null;

        ConvertChild(pRootOfTree);

        TreeNode head = pRootOfTree;

        while(head.left != null) {
            head = head.left;
        }
        return head;
    }


    public TreeNode lowestCommonAncestor(TreeNode root,
                                         TreeNode p, TreeNode q) {
        if(root == null) {
            return null;
        }
        if(root == p || root == q) {
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        if(leftTree != null && rightTree != null) {
            return root;
        }
        //代码执行到这里，说明：一个肯定是为空的
        if(leftTree != null) {
            return leftTree;
        }
        if(rightTree != null) {
            return rightTree;
        }
        //左右都为空
        return null;//没有最近公共祖先
    }

    /*
    class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode rgiht;

    public TreeNode(char val) {
        this.val = val;
    }
}


public class Main {
    public static int i = 0;
    public static TreeNode createTreeByString(String str) {
        if(str == null) return null;
        TreeNode root = null;
        if(str.charAt(i) != '#') {
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTreeByString(str);
            root.rgiht = createTreeByString(str);
        } else {
            i++;
        }
        return root;
    }


    public static void inorder(TreeNode root) {
        if(root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val+" ");
        inorder(root.rgiht);
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        TreeNode root = createTreeByString(str);
        inorder(root);
    }

    }
    */

    /*

     public int prindex = 0;
    public TreeNode buildTreeChild(int[] preorder,int[] inorder,int inbegin,int inend) {
        if(inbegin > inend) {
            return null;//左树 或者 右树 为空
        }
        TreeNode root = new TreeNode(preorder[prindex]);
        //找根节点在中序遍历的数组中的结果
        int rootIndex = findRootIndex(inorder,inbegin,inend,preorder[prindex]);
        prindex++;
        root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex-1);
        root.right = buildTreeChild(preorder,inorder,rootIndex+1,inend);
        return root;
    }
    //就是一个数组当中的查找代码
    public int findRootIndex(int[] inorder,int inbegin,int inend,int key) {
        for(int i = inbegin;i <= inend;i++) {
            if(inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder == null || inorder == null) return null;

        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }
     */

    /*
    public int postindex = 0;
    public TreeNode buildTreeChild(int[] postorder,int[] inorder,int inbegin,int inend) {
        if(inbegin > inend) {
            return null;//左树 或者 右树 为空
        }
        TreeNode root = new TreeNode(postorder[postindex]);
        //找根节点在中序遍历的数组中的结果
        int rootIndex = findRootIndex(inorder,inbegin,inend,postorder[postindex]);
        postindex--;

        root.right = buildTreeChild(postorder,inorder,rootIndex+1,inend);

        root.left = buildTreeChild(postorder,inorder,inbegin,rootIndex-1);

        return root;
    }
    //就是一个数组当中的查找代码
    public int findRootIndex(int[] inorder,int inbegin,int inend,int key) {
        for(int i = inbegin;i <= inend;i++) {
            if(inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(postorder == null || inorder == null) return null;
        postindex = postorder.length-1;
        return buildTreeChild(postorder,inorder,0,inorder.length-1);
    }
     */

    /*
    public void tree2strChild(TreeNode t,StringBuilder sb) {
        if(t == null) return ;
        sb.append(t.val);

        if(t.left == null) {
            if(t.right == null) {
                return;
            }else{
                sb.append("()");
            }
        } else {
            sb.append("(");
            tree2strChild(t.left,sb);
            sb.append(")");
        }

        if(t.right == null) {
            return;
        }else{
            sb.append("(");
            tree2strChild(t.right,sb);
            sb.append(")");
        }
    }

    public String tree2str(TreeNode root) {
        if(root == null) return null;
        StringBuilder sb = new StringBuilder();
        tree2strChild(root,sb);
        return sb.toString();
    }
     */
}
